//
//  GluSegVc.m
//  CareRingApp
//
//  Created  on 2025/2/13.
//
    

#import "GluSegVc.h"

#import <Masonry/Masonry.h>
#import <QMUIKit/QMUIKit.h>
#import "ConfigModel.h"
#import "GluMeasurVc.h"

#import "NestingScrollview.h"
#import "UIViewController+Custom.h"

@interface GluSegVc ()<UIScrollViewDelegate, UIGestureRecognizerDelegate>


@property(strong, nonatomic)NestingScrollview *mainScrollView; //主切换

@property(strong, nonatomic) GluMeasurVc *gluMeaVc;


@end

@implementation GluSegVc

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(appEnterBackground:)
                                                 name:UIApplicationDidEnterBackgroundNotification object:nil]; // 进入后天
    
    [self initUI];
    
    
}

-(void)appEnterBackground:(NSNotification *)notify
{
    if (self.gluMeaVc.status == GLU_STA_INIT)
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
    
}

-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.gluMeaVc.status == GLU_STA_INIT
            || self.gluMeaVc.status == GLU_STA_WAIT_RESULT_FAIL; // 测量中或等待
}

-(void)initUI
{
    WEAK_SELF
    [self customNavStyleNormal:@"Blood Glucose Assessment" BackBlk:^{
        STRONG_SELF
        BOOL canPop = strongSelf.gluMeaVc.status == GLU_STA_INIT
                        || strongSelf.gluMeaVc.status == GLU_STA_WAIT_RESULT_FAIL
                        ||  strongSelf.gluMeaVc.status == GLU_STA_UPLOAD_DATA_FAIL;
        NSString *tips = nil;
        if (strongSelf.gluMeaVc.status == GLU_STA_UPLOAD_DATA_FAIL)
        {
            tips = @"Are you sure you want to abandon the data upload?";
            
        }
        if (strongSelf.gluMeaVc.status == GLU_STA_WAIT_RESULT_FAIL)
        {
            tips = @"Are you sure you want to abandon this measurement result?";
        }
        
        if (tips) {
            [strongSelf showAlertWarningWithTitle:@"Tips" Msg:tips btnOk:@"Sure" OkBLk:^{
                
                if (canPop) {
                    [strongSelf.navigationController popViewControllerAnimated:YES];
                }
            } CancelBtn:_L2(L_CANCEL) CancelBlk:^{
                
            } Compelete:^{
                
            }];
            return;
        }
      
        if (!tips) {
            [strongSelf.navigationController popViewControllerAnimated:YES];

        }
    }];
    

    [self.view addSubview:self.mainScrollView];
    [self.mainScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.mainScrollView.superview.mas_safeAreaLayoutGuideTop).offset(0);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    
    [self.mainScrollView addSubview:self.gluMeaVc.view]; // 布局在 viewDidLayoutSubviews


  
    
    self.gluMeaVc.weakNavControl = self.navigationController;
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].idleTimerDisabled = YES; // 常亮

}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].idleTimerDisabled = NO; // 关闭常亮

}



-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    
    if (self.mainScrollView.delegate == nil) {
        self.mainScrollView.delegate = self;
        self.gluMeaVc.view.frame = CGRectMake(0, 0, self.mainScrollView.bounds.size.width, self.mainScrollView.bounds.size.height);
        
        self.mainScrollView.contentSize = CGSizeMake(self.mainScrollView.bounds.size.width, self.mainScrollView.bounds.size.height);
        
        
    }
}


-(void)segSelect:(UISegmentedControl *)seg
{
//    DebugNSLog(@"选择 seg %d",seg.selectedSegmentIndex);
    NSInteger idx = seg.selectedSegmentIndex;
    
    [self.mainScrollView setContentOffset:CGPointMake(self.mainScrollView.bounds.size.width * idx, 0) animated:YES];
    
}



#pragma mark --UIScrollViewDelegate
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
  
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  
    
}


-(NestingScrollview *)mainScrollView
{
    if (!_mainScrollView) {
        _mainScrollView = [[NestingScrollview alloc]init];
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.pagingEnabled = YES;
//        _mainScrollView.userInteractionEnabled = NO;
        //        _mainScrollView.delegate = self;
    }
    return  _mainScrollView;
}

-(GluMeasurVc *)gluMeaVc
{
    if (!_gluMeaVc) {
        _gluMeaVc = [[GluMeasurVc alloc]init];
        _gluMeaVc.glu_ltk = self.glu_ltk;
                
        [CARingSDK instance].bleDataDelegate = _gluMeaVc;
        [CARingSDK instance].blescanDelegate = _gluMeaVc;
        
        WEAK_SELF
        _gluMeaVc.stateChangeBLK = ^(GLU_STA state) {
            STRONG_SELF
            strongSelf.mainScrollView.scrollEnabled = (state == GLU_STA_INIT);
            
        };
    }
    return _gluMeaVc;
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
